from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from cfg.cfg_local_mysql import *
import json
from hyrobot.common import *

def get_global_webdriver():
    return GSTORE['global_webdriver']

def get_colum_list(driver,colum_num):
    table_loc = driver.find_element(By.CSS_SELECTOR,
                                    ".el-table"
                                     )
    # 按行查询表格的数据，取出的数据是一整行，按空格分隔每一列的数据
    table_tr_list = table_loc.find_elements(By.TAG_NAME, 'tr')
    # table_row_length = len(table_tr_list)
    # INFO(table_tr_list) 为引用
    name_list = []
    for i in table_tr_list[1:]:
        tmp = i.find_elements_by_class_name("cell")[colum_num].text
        name_list.append(tmp)
    # INFO(name_list)
    # ['NAME--25', 'NAME--25', 'NAME--28',
    return name_list

def get_current_page_insert_list(driver):
    # 获得当前页插入数据库的列表
    table_loc = driver.find_element(By.CSS_SELECTOR,
                                    ".el-table"
                                    )
    # 按行查询表格的数据，取出的数据是一整行，按空格分隔每一列的数据
    table_tr_list = table_loc.find_elements(By.TAG_NAME, 'tr')
    table_row_length = len(table_tr_list)
    # INFO(table_tr_list) 为引用
    insert_list1 = []
    for i in table_tr_list[1:table_row_length]:
        producut_name = i.find_elements_by_class_name("cell")[0].text
        version = i.find_elements_by_class_name("cell")[1].text
        creator = i.find_elements_by_class_name('cell')[2].text
        create_time = i.find_elements_by_class_name("cell")[3].text
        update_time = i.find_elements_by_class_name("cell")[4].text
        displaying = i.find_elements_by_class_name('cell')[5].text
        display_flag = 0
        if displaying == '显示':
            display_flag = 1
        tmp_tuple = (producut_name, version, creator, create_time, update_time, display_flag)
        insert_list1.append(tmp_tuple)

    return insert_list1

def get_current_page_insert_list_product_list(driver):
    # 获得当前页产品管理插入数据库的列表
    time.sleep(1)
    check_btn = driver.find_elements(By.CLASS_NAME, 'product-card-button-third')
    check_insert_list1 = []
    times = 1
    for tmp_btn in check_btn:
        INFO(times)
        times += 1
        time.sleep(1)
        tmp_btn.click()
        time.sleep(1)
        product_name = driver.find_element(By.CSS_SELECTOR,
                                           'div.el-dialog__wrapper:nth-child(3) > div:nth-child(1) > div:nth-child(1) > span:nth-child(1)'
                                           ).text
        dialog_list = driver.find_elements(By.CLASS_NAME, 'detail-text')
        value_list = []
        for value in dialog_list:
            inner_list = value.find_elements_by_tag_name('span')
            tmp = inner_list[1].text
            value_list.append(tmp)
        product_tuple = (
        product_name, value_list[0], value_list[1], value_list[2], value_list[3], value_list[4], value_list[5])
        check_insert_list1.append(product_tuple)
        # close
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR,
                            "span.dialog-footer > button:nth-child(1) > span:nth-child(1)"
                            ).click()
        time.sleep(1)
    return check_insert_list1

def logout(driver):
    time.sleep(2)
    driver.find_element(By.CSS_SELECTOR,
    ".avatar-wrapper > span:nth-child(1)").click()
    time.sleep(3)
    driver.find_element(By.CSS_SELECTOR,
    "li.el-dropdown-menu__item:nth-child(2) > span:nth-child(1)").click()

def mgr_login(wd):
    wd.get("http://172.17.66.19:8080/")

    wd.find_element(By.NAME, "username").send_keys("sunxidong")
    wd.find_element(By.NAME, "password").send_keys("123456")
    wd.find_element(By.CSS_SELECTOR, ".el-button").click()

def open_browser():

    print('打开浏览器')
    wd = webdriver.Firefox()
    wd.implicitly_wait(5)

    # 使用黑羽robot 全局存储对象 GSTORE
    GSTORE['global_webdriver'] = wd
    return wd

def productor1_login(wd):
    wd.get("http://172.17.66.19:8080/")

    wd.find_element(By.NAME, "username").send_keys("zhangsan")
    wd.find_element(By.NAME, "password").send_keys("123456")
    wd.find_element(By.CSS_SELECTOR, ".el-button").click()

def productor2_login(wd):
    wd.get("http://172.17.66.19:8080/")

    wd.find_element(By.NAME, "username").send_keys("lvdelin")
    wd.find_element(By.NAME, "password").send_keys("123456")
    wd.find_element(By.CSS_SELECTOR, ".el-button").click()

def super_login(wd):
    wd.get("http://172.17.66.19:8080/")
    wd.find_element(By.NAME, "username").send_keys('sunxidong')
    wd.find_element(By.NAME, "password").send_keys('123456')
    wd.find_element(By.CSS_SELECTOR, ".el-button").click()

def sum_row_num(driver):
    table_loc = driver.find_element(By.CSS_SELECTOR,
                                    '.el-table'
                                    )
    # 按行查询表格的数据，取出的数据是一整行，按空格分隔每一列的数据
    table_tr_list = table_loc.find_elements(By.TAG_NAME, 'tr')
    btn_num = len(table_tr_list) - 1
    return btn_num

def user_login(wd):
    wd.get("http://172.17.66.19:8080/")

    wd.find_element(By.NAME, "username").send_keys("editor3")
    wd.find_element(By.NAME, "password").send_keys("123456")
    wd.find_element(By.CSS_SELECTOR, ".el-button").click()
# 获取 全局使用的 webdriver 对象

def write_detail_into_file(driver,button_list,btn_length,file):
    """
    详细信息写入文件
    :param wd:  webdriver
    :param button_list: 按键列表
    :param btn_length: 按键总数量
    :param file：写入文件路径
    """
    times = 1
    for i in button_list[1:btn_length + 1]:
        INFO(times)
        times += 1
        time.sleep(2)
        i.click()
        dialog_text = driver.find_elements(By.CLASS_NAME, "applyRecord-text")
        dict = {}
        for list in dialog_text:
            list_text = list.find_elements(By.TAG_NAME, "span")
            tag_text = list_text[0].text
            value_text = list_text[1].text
            dict[tag_text] = value_text

        str_dict = json.dumps(dict)
        file.write(str_dict + '\n')
        time.sleep(1)
        # close
        driver.find_element(By.CSS_SELECTOR,
                            "div.el-dialog__wrapper:nth-child(4) > div:nth-child(1) > div:nth-child(1) > button:nth-child(2) > i:nth-child(1)"
                            ).click()
        time.sleep(1)



# from lib.webui import  *

# class c0101:
#     name = '管理员首页 - UI-0101' # 测试用例名字
#
# 初始化方法
#     def setup(self):
# #       STEP(1, '登陆网站')
#         wd = open_browser()
#         mgr_login(wd)
#
#     #清除方法
#     def teardown(self):
#         wd = get_global_webdriver()
#         wd.quit()
#
#     # 测试用例步骤
#     def teststeps(self):
#         # 获取webdriver对象 对应 已经登录好的浏览器
#         wd = get_global_webdriver()